package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.swla2.util.MinioTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("/minio")
public class MinioController {
    @Autowired
    private MinioTemplate minioTemplate;

    @Anonymous
    @PostMapping("/upload")
    public AjaxResult minioUploadFile(@RequestPart MultipartFile file) throws Exception {
        try {
            System.out.println("文件正在上传");
            String fileName = file.getOriginalFilename();
            String uuid = UUID.randomUUID().toString();
            String fileType = fileName.substring(fileName.lastIndexOf("."));
            String finalFileName = uuid + fileType;
            minioTemplate.putObject(finalFileName, file);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("path", finalFileName);
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
